package com.jy.servlet;

import com.alibaba.fastjson.JSON;
import com.jy.bean.User;
import com.jy.dao.UserDao;
import com.jy.service.UserService;
import com.jy.service.impl.UserServicelmpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class UseerServlet extends HttpServlet {
    private static final Logger log = LoggerFactory.getLogger(UserDao.class);
    private UserService userService;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String Email =  request.getParameter("Email");
        String Password = request.getParameter("password");
        String name = request.getParameter("user_name");
        userService = new UserServicelmpl();
        String state = "";
        if (userService.seleuser(Email,name) == true){
            System.out.println("xhcs"+1);
            state = "当前邮箱号或账号以被注册请重新输入";
        }else if (userService.seleuser(Email,name) == false){
            System.out.println("xhcs"+2);
            if (userService.register(new User(name,Email,Password))){
                state = "注册成功！！";
                log.debug("注册成功");
            }else {
                log.debug("注册失败");
            }
        }
        String data = JSON.toJSONString(state);

        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        out.println(data);
        log.debug("data:" +data);
    }
}
